#include <string>
#include <unordered_map>
#include <fstream>
#include "log.hpp"

const std::string defaultFilePath="./dict.txt";
const std::string separator=": ";//分隔符

class Dict
{
public:
    Dict(const std::string&filePath=defaultFilePath)
        :_filePath(filePath)
    {
        Load();
    }

    std::string translate(const std::string&word)
    {
        auto iterator=_dict.find(word);
        if(iterator==_dict.end())
        {
            return "";
        }
        return _dict[word];
    }
private:
    std::string _filePath;
    std::unordered_map<std::string,std::string>_dict;

    void Load()
    {
        std::ifstream in(_filePath);
        std::string message;
        while(std::getline(in,message))
        {
            int pos=message.find(separator);
            if(pos==std::string::npos)continue;
            std::string word=message.substr(0,pos);
            std::string chinese=message.substr(pos+2);
            _dict.insert(std::make_pair(word,chinese));
            Log(DEBUG,"%s:%s has been inserted into the dict",word.c_str(),chinese.c_str());
        }
        in.close();
        Log(DEBUG,"The loading has been successful");
    }
};